package inits

import (
	"regexp"

	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
)

func InitValidator() {
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("validatorUsername", ValidatorUsername)
	}
}

var ValidatorUsername validator.Func = func(fl validator.FieldLevel) bool {
	username := fl.Field().String()
	//log.Printf("username: %s", username)
	reg := regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9_]{7,15}$")
	return reg.MatchString(username)
}
